<?php

namespace app\admin\controller;

use app\common\controller\Adminbase;
use think\Request;

class Level extends Adminbase
{
    protected $add_edit_field=[];
    public function initialize(){
        parent::initialize();
        $this->add_edit_field=[
            ['field'=>'title','title'=>'等级名称','type'=>'text','verify'=>'required','order_by'=>1],
            ['field'=>'pic','title'=>'等级图片','type'=>'file_img','tips'=>'为等级设置一个图片'],
            ['field'=>'level','title'=>'等级标识','type'=>'number','verify'=>'required|number','tips'=>'等级的排序标识需大于上一级或小于上一级，不可重复'],
            ['field'=>'money','title'=>'所需充值','type'=>'number','verify'=>'number','tips'=>'设置一个充值达标额度'],
            ['field'=>'integrals','title'=>'所需积分','type'=>'number','verify'=>'number','tips'=>'设置一个积分达标额度'],
            ['field'=>'requirement','title'=>'晋级方式','type'=>'radio','tips'=>'选择用户晋级方案','data'=>['1'=>'全部达标','2'=>'充值达标','3'=>'积分达标'],'value'=>'1'],
            ['field'=>'recharge_gift','title'=>'充值赠送','type'=>'number','verify'=>'number','tips'=>'等级充值赠送百分比','value'=>'0'],
            ['field'=>'money_discount','title'=>'消费余额折扣','type'=>'number','verify'=>'number','tips'=>'消费余额折扣百分比','value'=>'0'],
            ['field'=>'integrals_discount','title'=>'消费积分折扣','type'=>'number','verify'=>'number','tips'=>'消费积分折扣百分比','value'=>'0'],
            ['field'=>'status','title'=>'等级状态','type'=>'radio','tips'=>'等级是否可用','data'=>['1'=>'正常','2'=>'封禁'],'value'=>'1']
        ];
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $info = db('level')->order('level asc')->select();
        if($info){
            foreach ($info as $k=>$v){
                $requirement = ['1'=>'全部达标','2'=>'充值达标','3'=>'积分达标'];
                $info[$k]['requirement'] = $requirement[$v['requirement']];
            }
        }
        $filed = [
            ['field'=>'id','title'=>'ID'],
            ['field'=>'title','title'=>'等级名称'],
            ['field'=>'pic','title'=>'等级图标','template'=>'img'],
            ['field'=>'level','title'=>'等级标识'],
            ['field'=>'money','title'=>'所需充值'],
            ['field'=>'integrals','title'=>'所需积分'],
            ['field'=>'requirement','title'=>'晋级方式'],
            ['field'=>'recharge_gift','title'=>'充值赠送'],
            ['field'=>'money_discount','title'=>'消费余额折扣'],
            ['field'=>'integrals_discount','title'=>'消费积分折扣'],
            ['field'=>'status','title'=>'等级状态'],
            ['field'=>'create_at','title'=>'添加时间'],
            ['field'=>'update_at','title'=>'更新时间'],
        ];
        return view('public/list',['filed'=>$filed,'info'=>json_encode($info,JSON_UNESCAPED_UNICODE)]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function save()
    {
        return view('public/add',['filed'=>$this->add_edit_field]);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function add(Request $request)
    {
        $data = $request->param();
        $verify = add_edit_verify($data,['title','level'],['level'],'level');
        if($verify['code'] != 200){
            return json($verify);
        }
        return json($this->add_info('level',$data));
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function edit(Request $request)
    {
        $id = $request->param('id');
        if(!$id){
            return json(['code'=>1,'msg'=>'数据错误，need ID']);
        }
        $data = db('level')->where(['id'=>$id])->find();
        if(!$data){
            return json(['code'=>1,'msg'=>'数据不存在']);
        }
        return view('public/edit',['filed'=>$this->add_edit_field,'info'=>$data]);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function update(Request $request)
    {
        $data = $request->param();
        $verify = add_edit_verify($data,['id','title','level'],['level'],'level');
        if($verify['code'] != 200){
            return json($verify);
        }
        $res = $this->update_info('level',$data);
        return json($res);
    }

    /**
     * 删除指定资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function delete(Request $request)
    {
        $id = $request -> post('id');
        if(!is_array($id)){
            if(!is_numeric($id)){
                return json(['code'=>1,'msg'=>'数据错误']);
            }else{
                $id = explode(',',$id);
            }
        }
        if(is_array($id)){
            foreach($id as $val){
                $old = db('level')->where('id',$val)->find();
                if(!$old){
                    return json(['code'=>1,'msg'=>'存在非数据库数据，请核查']);
                }
            }
            $res = db('level')->delete($id);
            if($res){
                return json(['code'=>200,'msg'=>'删除成功']);
            }else{
                return json(['code'=>1,'msg'=>'删除失败']);
            }
        }else{
            return json(['code'=>1,'msg'=>'数据错误']);
        }
    }
}
